package cn.itcast.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CommonConfig{

    public static final String DIRECT_EXCHANGE = "direct.exchange";
    public static final String DIRECT_ROUTE_KEY = "direct.key";
    public static final String DIRECT_QUEUE = "direct.queue";


    @Bean
    public DirectExchange directExchange(){
        //return new DirectExchange(DIRECT_EXCHANGE);//交换机持久化第一种方式 ：单参构造方法
        //return new DirectExchange(DIRECT_EXCHANGE,true,false);//交换机持久化第二种方式 ：强制制定持久化
        return ExchangeBuilder.directExchange(DIRECT_EXCHANGE).durable(true).build();//交换机持久化第三种方式 ：强制制定持久化
    }

    @Bean
    public Queue directQueue(){
        //return new Queue(DIRECT_QUEUE);//持久化写法1（默认）
        //return new Queue(DIRECT_QUEUE,true);//持久化写法2
        return QueueBuilder.durable(DIRECT_QUEUE).build();//持久化写法3
    }

    @Bean
    public Binding bindDirectQueue(){
        return BindingBuilder.bind(directQueue()).to(directExchange()).with(DIRECT_ROUTE_KEY);
    }
}
